package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 攻略明细控制器
 */
@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    //  设置攻略阅读数
    @GetMapping("/detail")
    public Object detail(String id){
        Strategy strategy = strategyService.selectById(id);
        strategyStatisVORedisService.viewnumIncrease(id, 1);
        return JsonResult.success(strategy);
    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }

    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.selectAll());
    }

    //  攻略评论方法
    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(@UserParam UserInfo userInfo, StrategyComment strategyComment){
        //  此工具方法可以用于拷贝属性
        //  前提是源和目标中的属性必须一致
        //  若不一致，需要手动设置
        BeanUtils.copyProperties(userInfo, strategyComment);
        //  这个就是不一致的用户id
        strategyComment.setUserId(userInfo.getId());
        //  插入目标对象
        strategyCommentService.insert(strategyComment);
        strategyStatisVORedisService.replynumIncrease(strategyComment.getStrategyId(), 1);
        //  返回一个状态
        return JsonResult.success();
    }

    //  攻略评论分页方法
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    //  攻略评论点赞/取消点赞
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(@UserParam UserInfo userInfo, String cid){
        strategyCommentService.commentThumb(userInfo.getId(), cid);
        return JsonResult.success();
    }

    //  获取攻略阅读数
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }

    //  设置收藏成功/取消收藏
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.setFavornum(sid, userInfo.getId());
        return JsonResult.success(ret);
    }

    //  设置顶操作
    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.setThumbsupnum(sid, userInfo.getId());
        return JsonResult.success(ret);
    }

}
